<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/8/25
 * Time: 11:18
 */

namespace ticket\libs;

use ticket\modules\frontend\models\ErrorMsg;
use ticket\modules\frontend\models\PayConfig;
use Yii;

/*** 支付类
 * Class PayOrder
 * @package ticket\libs
 */
class PayOrder
{

    public function perOrder($params)
    {
//        $payData = Yii::$app->params['AppId_'.$params['app_id']];
        $payData = $this->getPayConfig($params['app_id']);
        if (empty($payData)){
            return false;
        }
        $callback = Yii::$app->params['SITE_URL'].'/frontend/pay-callback/ticket-callback';
        $preDataInfo = [
            'channelMerId' => $payData['channelMerId'],
            'appId' => $payData['appId'],
            'channelId' => $payData['channelId'],
            'nonceStr' => Tools::createStr(18),
            'timestamp' => time(),
            'userId' => $params['openid'],
            'aid' => $payData['aid'],
            'callBackUrl' => $callback,
            'from' => '票务-门票',
            'amount' => $params['real_pay'],
            'payNo' => $params['order_number'],
            'commodityName' => $params['goods_name'],
            'signType' => 'MD5',
            'tradeType' => 'JSAPI',
            'attach' => $params['app_id'],
        ];

        $sign = Tools::computeSign($preDataInfo,$payData['key']);
        $preDataInfo['sign'] = $sign;

        $payUrl = Yii::$app->params['Pay_Url'];

        $result = Curl::post($payUrl,$preDataInfo);
        ErrorMsg::setInfo($_SERVER['REQUEST_URI'],99999,'支付-获取支付参数'.json_encode($preDataInfo,JSON_UNESCAPED_UNICODE),$result);
        return $result;
    }


    /*** 退款
     * @param $params
     * @param $refundNum
     * @return bool|mixed
     */
    public function refundOrder($params,$refundNum)
    {
        $payData = $this->getPayConfig($params['app_id']);
        if (empty($payData)){
            return false;
        }

        $refDataInfo = [
            'channelMerId' => $payData['channelMerId'],
            'appId' => $payData['appId'],
            'aid' => $payData['aid'],
            'payUserId' => $params['openid'],
            'nonceStr' => Tools::createStr(18),
            'timeStamp' => time(),
            'payUserSerial' => $params['order_number'],
            'totalFee' => $params['order_amount'],
            'refundFee' => $params['order_amount'],
            'refundUserSerial' => $refundNum,
            'signType' => 'MD5',
            'refundType' => 'moon',
        ];
        $sign = Tools::computeSign($refDataInfo,$payData['key']);
        $refDataInfo['sign'] = $sign;
        $rfdUrl = Yii::$app->params['Rfd_Url'];
        $result = json_decode(Curl::post($rfdUrl,$refDataInfo),true);
        ErrorMsg::setInfo($_SERVER['REQUEST_URI'],99999,'退款- 参数'.json_encode($refDataInfo,JSON_UNESCAPED_UNICODE),json_encode($result,JSON_UNESCAPED_UNICODE));
        return $result;


    }



    private function getPayConfig($appId)
    {
        $payConfigModel = new PayConfig();
        $payData = $payConfigModel->getInfo($appId);
        return $payData;
    }



    public function perAliOrder($params)
    {
        $payData = $this->getPayConfig(3);
        if (empty($payData)){
            return false;
        }
        $callback = Yii::$app->params['SITE_URL'].'frontend/pay-callback/ali-ticket-callback';
        $preDataInfo = [
            'channelMerId' => $payData['channelMerId'],
            'appId' => $payData['appId'],
            'channelId' => $payData['channelId'],
            'nonceStr' => Tools::createStr(18),
            'timestamp' => time(),
            'ali_user_id' => $params['openid'],
            'aid' => $payData['aid'],
            'callBackUrl' => $callback,
            'returnUrl' => 'http://pay.raysfly.com/frontend/pay-callback/kly-call-ali-back-url',
            'from' => '票务-门票-支付宝',
            'amount' => $params['real_pay'],
            'payNo' => $params['order_number'],
            'commodityName' => $params['goods_name']?$params['goods_name']:'恐龙园一日票',
            'subject' => $params['goods_name']?$params['goods_name']:'恐龙园一日票',
            'signType' => 'MD5',
            'tradeType' => 'facetoface',
            'attach' => '支付宝小程序门票订单',
        ];

        $sign = Tools::computeSign($preDataInfo,$payData['key']);
        $preDataInfo['sign'] = $sign;

        $payUrl = Yii::$app->params['Ali_Pay_Url'];

        $result = Curl::post($payUrl,$preDataInfo);
        ErrorMsg::setInfo($_SERVER['REQUEST_URI'],99999,'支付-获取支付参数'.json_encode($preDataInfo,JSON_UNESCAPED_UNICODE),$result);
        return $result;
    }



















}